import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/components/Login'
// 导入后台home组件
import Home from '@/components/home/Home.vue'
import Welcome from '@/components/home/Welcome.vue'
import UserList from '@/components/users/UserList.vue'
import Goods from '@/components/goods/Goods.vue'
import GoodsAdd from '@/components/goods/GoodsAdd.vue'
import Rights from '@/components/power/Rights.vue'
import Roles from '@/components/power/Roles.vue'
import Categories from '@/components/goods/Categories.vue'
import Params from '@/components/params/Params.vue'
import Order from '@/components/order/Order.vue'
import Reports from '@/components/reports/Reports.vue'

// const Home = () => import('@/components/home/Home.vue')
// const Welcome = () => import('@/components/home/Welcome.vue')
// const UserList = () => import('@/components/users/UserList.vue')
// const Goods = () => import('@/components/goods/Goods.vue')
// const GoodsAdd = () => import('@/components/goods/GoodsAdd.vue')
// const Rights = () => import('@/components/power/Rights.vue')
// const Roles = () => import('@/components/power/Roles.vue')
// const Categories = () => import('@/components/goods/Categories.vue')
// const Params = () => import('@/components/params/Params.vue')
// const Order = () => import('@/components/order/Order.vue')
// const Reports = () => import('@/components/reports/Reports.vue')

Vue.use(Router)

// 创建路由对象
const router = new Router({
  routes: [
    { path: '/', redirect: '/home' },
    { path: '/login', component: Login },
    {
      path: '/home',
      component: Home,
      redirect: '/welcome',
      children: [
        { path: '/reports', component: Reports },
        { path: '/orders', component: Order },
        { path: '/roles', component: Roles },
        { path: '/params', component: Params },
        { path: '/welcome', component: Welcome },
        { path: '/users', component: UserList },
        { path: '/goods', component: Goods },
        { path: '/goodsadd', component: GoodsAdd },
        { path: '/rights', component: Rights },
        { path: '/categories', component: Categories }
      ]
    }
  ]
})

// 路由导航守卫，只有登录用户才可以访问后台home路由
router.beforeEach((to, from, next) => {
  // console.log(to.path)
  // return null
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  // 没有token要跳转到login登录页面(有以下两种方式实现)
  if (!tokenStr) return next('/login')
  // if (!tokenStr) return this.a.push('/login')
  // 直接放行
  next()
})

// 暴露路由对象
export default router
